package d2_com.itheima.io;

import java.io.*;

public class Test2 {
    public static void main(String[] args) {
        InputStream fo = null;
        OutputStream fi = null;
        /**
         * 释放资源 try-catch-finally
         */

        /**
         * 复制照片
         */
        try {
            fo = new FileInputStream("oop-app6\\3.txt");
            fi = new FileOutputStream("oop-app6\\4.txt");
            byte[] tp = fo.readAllBytes();
            fi.write(tp);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //释放资源的操作
            try {
                if (fo != null) fo.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                if (fi != null) fi.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        /**
         * 释放资源 try-with-resource
         */
        //资源一般指的是最终实现了AutoCloseable接口。
        try (InputStream fo2 = new FileInputStream("oop-app6\\4.txt"); //只能放资源对象（流对象）自动释放资源 自动调用Close方法进行资源释放
             OutputStream fi2 = new FileOutputStream("oop-app6\\5.txt")) {
            byte[] tp = fo2.readAllBytes();
            fi2.write(tp);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
